package com.uab.ia.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.dataframework.Entity;
import com.uab.ia.R;
import com.uab.ia.entidades.Conexion;

public class ConexionesAdapter extends BaseAdapter{

	private Context ctx;
	private ArrayList<Conexion> array;
	
	public ConexionesAdapter(Context ctx, ArrayList<Conexion> objects) {
		this.ctx = ctx;
		this.array = objects;
	}
	
	public int getCount() {
		return array.size();
	}

	public Conexion getItem(int arg0) {
		return array.get(arg0);
	}

	public long getItemId(int position) {
		return array.get(position).getId();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		
		if ((convertView == null) || !(convertView.getTag() instanceof ViewHolder))
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = (LinearLayout) inflater.inflate(R.layout.row_conexion, null);
			
			holder = new ViewHolder();

			holder.txt_e1 = (TextView) convertView.findViewById(R.id.text_e1);
			holder.txt_e2 = (TextView) convertView.findViewById(R.id.text_e2);
			holder.txt_conected = (TextView) convertView.findViewById(R.id.text_conected);
			holder.txt_tiempo = (TextView) convertView.findViewById(R.id.text_tiempo);
			
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}

		Conexion conex = array.get(position);
		
		String e1 = new Entity("estacion", conex.getId_e1()).toString();
		String e2 = new Entity("estacion", conex.getId_e2()).toString();
		
		Log.i("IA", e1 + " - " + e2);
		
		holder.txt_e1.setText(e1);
		holder.txt_e2.setText(e2);
		holder.txt_tiempo.setText(Integer.toString(conex.getTime()));
		holder.txt_conected.setText(Boolean.toString(conex.isConected()));
		
		return convertView;
	}

	private class ViewHolder {
		TextView txt_e1;
		TextView txt_e2;
		TextView txt_tiempo;
		TextView txt_conected;
	}
}
